package com.example.conexion;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;


public class comprobarAlarma {
	private static final String url = "http://alertavecinal.zapto.org/WS/estadoAlarma.php";
	JSONParser jsonParser = new JSONParser();
	String idUsuario, idRed;
	public comprobarAlarma(String idRed, String idUsuario){
		this.idRed=idRed;
		this.idUsuario=idUsuario;
	}
	public estadoAlarma comprobar(){
	boolean success;
	estadoAlarma estado=new estadoAlarma();
	try {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("idRed", idRed));
		params.add(new BasicNameValuePair("idUsuario", idUsuario));

		// getting product details by making HTTP request
		// Note that product details url will use GET request
		JSONObject json = jsonParser.makeHttpRequest(
				url, "POST", params);

		// check your log for json response
		Log.d("ESTADO DE ALARMA", json.toString());
		
		// json success tag
		success =Boolean.parseBoolean( json.getString("success"));
		
		if (success) {
			// successfully received product details
			JSONArray productObj = json
					.getJSONArray("alarmas"); // JSON Array
			
			// get first product object from JSON Array
			JSONObject valores = productObj.getJSONObject(0);
            estado.setEstado(true);
            estado.setTipoAlarma(valores.getString("tipoAlarma"));
            estado.setIdAlarma(valores.getString("idAlarma"));

		}else{
			// product with pid not found
		}
	} catch (JSONException e) {
		e.printStackTrace();
	}
return estado;
}
}
